package es.deusto.engineering.is.kbs.snakes;

import org.drools.KnowledgeBase;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import es.deusto.engineering.is.kbs.snakes.Snake.Color;
import es.deusto.engineering.is.kbs.snakes.Snake.Fangs;
import es.deusto.engineering.is.kbs.snakes.Snake.Feature;
import es.deusto.engineering.is.kbs.snakes.Snake.Pattern;
import es.deusto.engineering.is.kbs.snakes.Snake.Scales;
import es.deusto.engineering.is.kbs.util.KnowledgeBaseLoader;

public class InferenceEngine {

	public static void main(String[] args) {
		//Se crean 5 instancias de snakes
		Snake snake1 = new Snake();
		snake1.setIdentifier("Snake 1");
		snake1.setFangs(Fangs.FRONT_FIXED);
		snake1.setPit(true);
		snake1.setLength(2d);
		snake1.setCircumference(snake1.getLength()/9);
		snake1.setScales(Scales.SMOOTH);
		snake1.setPattern(Pattern.RED_YELLOW_BLACK_BANDS);
		
		Snake snake2 = new Snake();
		snake2.setIdentifier("Snake 2");
		snake2.setFangs(Fangs.FRONT_MOVABLE);		
		snake2.setLength(4.5d);
		snake2.setPit(false);
		snake2.setCircumference(snake2.getLength()/4);
		snake2.setScales(Scales.KEELED);
		snake2.addFeature(Feature.PROTUBERANT_HEAD_NOSTRILS);
		snake2.setColor(Color.LIGHT_BROWN);
		snake2.setPattern(Pattern.DIAMOND_SHAPED_BROWN_BLOTCHES);
		
		Snake snake3 = new Snake();
		snake3.setIdentifier("Snake 3");
		snake3.setFangs(Fangs.FRONT_MOVABLE);		
		snake3.setLength(2d);
		snake3.setPit(true);
		snake3.setCircumference(snake3.getLength()/5);
		snake3.setScales(Scales.SMOOTH);		
		snake3.setColor(Color.BROWN);
		snake3.setPattern(Pattern.DARK_BROWN_BANDS);
		
		Snake snake4 = new Snake();
		snake4.setIdentifier("Snake 4");
		snake4.setFangs(Fangs.FRONT_MOVABLE);		
		snake4.setLength(2.5d);		
		snake4.setCircumference(snake4.getLength()/5);
		snake4.setPit(true);
		snake4.setScales(Scales.KEELED);
		snake4.addFeature(Feature.TAIL_RATTLE);
		snake4.setColor(Color.BROWN);
		snake4.setPattern(Pattern.DARK_BROWN_BANDS);

		Snake snake5 = new Snake();
		snake5.setIdentifier("Snake 5");
		snake5.setFangs(Fangs.REAR);		
		snake5.setLength(3d);
		snake5.setPit(false);
		snake5.setCircumference(snake5.getLength()/10);
		snake5.setScales(Scales.SMOOTH);
		snake5.addFeature(Feature.BIG_ROUND_PUPILS);
		snake5.setPattern(Pattern.UNIFORM_COLOR);		
		
		System.out.println("BEFORE THE INFERENCE PROCESS");
		System.out.println("------ --- --------- -------");
		System.out.println(snake1);
		System.out.println("\n" + snake2);
		System.out.println("\n" + snake3);
		System.out.println("\n" + snake4);
		System.out.println("\n" + snake5);			
		
		// The knowledge base made up of a rules file ".drl", is loaded 
		KnowledgeBase kBase = KnowledgeBaseLoader.loadKnowledgeBase("rules.drl");

		// the working memory is created. Drools calls it Knowledge session
		StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession();

		//the log for the inference process is initialized
		KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(kSession, "log/log");
		
		//Objets are inserted into the Working Memory
		kSession.insert(snake1);
		kSession.insert(snake2);
		kSession.insert(snake3);
		kSession.insert(snake4);
		kSession.insert(snake5);
		
		System.out.println("\nDURING THE INFERENCE PROCESS");
		System.out.println("------ --- --------- -------");

		//Perform inference process
		kSession.fireAllRules();
		
		//Close the log
		logger.close();

		//After the inference process
		System.out.println("\nAFTER THE INFERENCE PROCESS");
		System.out.println("  ----- --- --------- -------");
		System.out.println(snake1);
		System.out.println("\n" + snake2);
		System.out.println("\n" + snake3);
		System.out.println("\n" + snake4);
		System.out.println("\n" + snake5);		
	}
}